package logic

import (
	"context"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AgentByIdCardLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAgentByIdCardLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AgentByIdCardLogic {
	return &AgentByIdCardLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 根据身份证号查询代理商
func (l *AgentByIdCardLogic) AgentByIdCard(in *agentClient.AgentByIdCardReq) (*agentClient.AgentParam, error) {
	agent, err := l.svcCtx.AgentModel.FindOneByIdCard(l.ctx, in.IdCard)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, errorx.NewDefaultError("用户不存在。")
		}
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &agentClient.AgentParam{
		Id:                      agent.Id,
		Avatar:                  agent.Avatar,
		Name:                    agent.Name,
		Gender:                  agent.Gender,
		Phone:                   agent.Phone,
		Status:                  agent.Status,
		Type:                    agent.Type,
		IdCard:                  agent.IdCard,
		RealName:                agent.RealName,
		Company:                 agent.Company,
		BusinessLicense:         agent.BusinessLicense,
		UnifiedSocialCreditCode: agent.UnifiedSocialCreditCode,
		CreateTime:              agent.CreateTime.Unix(),
		CreateBy:                agent.CreateBy,
		UpdateTime:              agent.UpdateTime.Unix(),
		UpdateBy:                agent.UpdateBy,
		DelFlag:                 agent.DelFlag,
		OpenId:                  agent.OpenId,
		UnionId:                 agent.UnionId,
		StoreNum:                agent.StoreNum,
		ParentUid:               agent.ParentUid,
		TopUid:                  agent.TopUid,
		ProfitRate:              agent.ProfitRate,
		CardRate:                agent.CardRate,
		InviteUid:               agent.InviteUid,
	}, nil
}
